package com.admins.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author mis_wu
 * @date 2022/1/8 13:48
 * 队列配置
 */
@Configuration
public class RabbitQueueConfig {
    @Bean
    public Queue queue(){
        // durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        // exclusive:默认也是false，只能被当前创建的连接使⽤，⽽且当连接关闭后队列即被删除。此参考优先级⾼于durable
        // autoDelete:是否⾃动删除，当没有⽣产者或者消费者使⽤此队列，该队列会⾃动删除。
        //   return new Queue("admins-mq",true,true,false);
        //⼀般设置⼀下队列的持久化就好,其余两个就是默认false
        return new Queue("admins-mq",true);
    }

    /**
     * Direct交换机起名：queueDirectExchange
     * @return
     */
    @Bean
    DirectExchange queueDirectExchange() {
        return new DirectExchange("queueDirectExchange",true,false);
    }

    /**
     * 绑定将队列和交换机绑定, 并设置⽤于匹配键：queueDirectRouting
     * @return
     */
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(queue()).to(queueDirectExchange()).with("queueDirectRouting");
    }

    @Bean
    DirectExchange lonelyDirectExchange() {
        return new DirectExchange("lonelyDirectExchange");
    }
}
